﻿package cn.poco.pepsi.adapter;

import java.util.HashMap;
import java.util.List;

import cn.poco.pepsi.R;
import cn.poco.pepsi.bean.Comment;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CommentAdapter extends BaseAdapter {
	
	/** constant **/
//	private final static String TAG = "CommentAdapter";
	
	public static HashMap<Integer,Boolean> flags = new HashMap<Integer,Boolean>();
	
	private List<Comment> comments; 
	private LayoutInflater myInflater;
	
	private Boolean lastColumn;
	
	public CommentAdapter(Context context,List<Comment> comments,Handler handler){
		this.comments = comments;
		myInflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		return comments.size();
	}

	@Override
	public Object getItem(int position) {
		return comments.get(position);
	}

	@Override
	public long getItemId(int position) {
		return comments.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolderHead view = null;
		if(convertView==null){
			convertView = myInflater.inflate(R.layout.res_comment_tab_items_ps, null);
			view = new ViewHolderHead();
			view.title = (TextView)convertView.findViewById(R.id.title);
			view.average = (TextView)convertView.findViewById(R.id.average);
			view.content = (TextView)convertView.findViewById(R.id.content);
			view.morecomments = (TextView)convertView.findViewById(R.id.morecomments);
			view.nextpage = (LinearLayout)convertView.findViewById(R.id.nextpage);
			convertView.setTag(view);
		}else{
			view = (ViewHolderHead)convertView.getTag();
		}
		
		
		//title
		String level =comments.get(position).getLevel();
		String id = comments.get(position).getName()+"";
		if(id.equals("") || id.equals("0")){
			id = "匿名";
		}
		String title = "";
		if(level!=null && !level.equals("")){
			title = id+"("+level+")";
		}else{
			title = id;
		}
		
		view.title.setText(title);
		//average
		String average = comments.get(position).getAverage();
		if(average!=null && !average.equals("") && !average.equals("0")){
			view.average.setText("人均:"+comments.get(position).getAverage());
		}
		if(flags.containsKey(position) && flags.get(position)==true){
			 view.content.setText(comments.get(position).getContent());
			 view.morecomments.setVisibility(View.GONE);
			  
		}else if(flags.containsKey(position) && flags.get(position)==false){
			String content = comments.get(position).getContent();
			int size = content.length();
			if(size>101){
				 view.content.setText(content.subSequence(0, 100));
				 view.morecomments.setVisibility(View.VISIBLE);
			}else{
				 view.content.setText(content);
			} 
		}else{
			String content = comments.get(position).getContent();
			int size = content.length();
			if(size>101){
				 view.content.setText(content.subSequence(0, 100));
				 view.morecomments.setVisibility(View.VISIBLE);
			}else{
				 view.content.setText(content);
			} 
			
		}
		
	/*	//下一页BAR
		if(position==comments.size()-1){
			if(!getLastColumn()){
				view.nextpage.setVisibility(View.VISIBLE);
				view.nextpage.
					setLayoutParams(new LinearLayout.LayoutParams(
							LayoutParams.FILL_PARENT,
							LayoutParams.WRAP_CONTENT));
			}

		}
		
		view.nextpage.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_UP:
					handler.sendEmptyMessage(Constants.NEXT_PAGE);
					break;
				}
				return true;
			}
		});*/
		return convertView;
	}

	
	 /* public void toggle(int position) {
		  if(flags.containsKey(position) && flags.get(position)==true){
			  flags.put(position, false); 
		  }else{
			  flags.put(position, true); 
		  }
          notifyDataSetChanged();
      }*/
	
	public final class ViewHolderHead{
		 public TextView title;
		 public TextView average;
		 public TextView content;
		 public TextView morecomments;
		 public LinearLayout nextpage;
		}
	
	public Boolean getLastColumn() {
		return lastColumn;
	}
	public void setLastColumn(Boolean lastColumn) {
		this.lastColumn = lastColumn;
	}
	
	

}
